package com.inspector.app.di.persistence.plot

import android.os.Parcelable
import androidx.room.*
import com.inspector.app.di.util.UI
import kotlinx.parcelize.Parcelize
import java.util.Date

/**
 * 地图标绘数据Entity
 * @author CodeK
 */
@Entity(indices = [Index(value = ["name", "visible", "delete", "upload", "uploaded"], unique = true)])
@Parcelize
data class MapItem (
    @PrimaryKey(autoGenerate = true)
    var mapID: Long? = null,

    @ColumnInfo(name = "flyElementId")
    var flyElementId: String? = null,   //[标绘id]

    @ColumnInfo(name = "coordinate", defaultValue = "WGS84")
    var coordinate: String = "WGS84",   //[坐标系类型"coordinate": "CGCS2000", //坐标系,例如:GCJ02，WGS84谷歌卫星图,GCJ02高德腾讯地]

    @ColumnInfo(name = "name", defaultValue = "")
    var name: String = "",              //[名称]
    @ColumnInfo(name = "fileName")      //[分组名称]
    var fileName: String? = null,

    @ColumnInfo(name = "elementType")
    var elementType: String? = null,   //[类型 0点 1线 2圆心 3多边形 4椭圆]

    @ColumnInfo(name = "layerType")
    var layerType: String? = null,     //[图层控制类型,航线0、航点1、空域2、禁飞区3、障碍物4 与MapLayerInstance中类型,一一对应成功]

    @ColumnInfo(name = "points")
    @TypeConverters(MapDaoConverters::class)
    var points: MutableList<MapPoint>? = null, //[点集合数据]

    @ColumnInfo(name = "status", defaultValue = "3")
    var status: String = "3",         //状态，0下发，1不下发，2共享，3不共享 私人默认3

    @ColumnInfo(name = "belongUser", defaultValue = "")
    var belongUser: String = "",      //[共享或者下发人]

    @ColumnInfo(name = "jsonType", defaultValue = "1")    //[默认数据类型 种类，1个人，2同事共享，3公司下发]
    var jsonType: String = "1",

    @ColumnInfo(name = "visible", defaultValue = "1")
    var visible: Boolean = true,      //[当前是否可见,默认可见]
    @ColumnInfo(name = "delete", defaultValue = "0")
    var delete: Boolean = false,      //[当前是否删除,默认未删除]
    @ColumnInfo(name = "upload", defaultValue = "0")
    var upload: Boolean = false,      //[当前任务上传状态,默认未上传待上传 true为已上传,已上传任务不需要处理，未上传时打开app或恰当时机处理并在处理后更新为true]
    @ColumnInfo(name = "uploaded", defaultValue = "0")
    var uploaded: Boolean = false,    //[是否之前上传过代码服务器,删除时需要判断,未上传过,走批量更新会造成整批数据上传失败]

    @ColumnInfo(name = "createTime")
    var createTime: String? = null,   //[创建时间 "2024-04-08 08:59:56"]
    @ColumnInfo(name="updateTime")
    var updateTime: String? = null,   //[更新时间 "2024-04-08 08:59:56"]


    @ColumnInfo(name="fillColor")
    var fillColor: String? = null,
    @ColumnInfo(name="fillAlpha")
    var fillAlpha: String? = null,
    @ColumnInfo(name="strokeColor")
    var strokeColor: String? = null,
    @ColumnInfo(name="strokeWidth")
    var strokeWidth: String? = null,

    @ColumnInfo(name="radius")
    var radius: String? = null,     //[圆半径]

): Parcelable {
    @Ignore
    var groupHeader: Boolean = false             //[页面布局是否为分组头部布局]
    @Ignore
    var groupName: String? = null                //[分组名称]
    @Ignore
    var groupItems: MutableList<MapItem>? = null //[分组下的集合数据]
    @Ignore
    var groupExpanded: Boolean = false          //[分组当前展开状态,默认收起]

    //[是否共享]
    @Ignore
    fun isShared(): Boolean {
        return status == "2"
    }

    @Ignore
    fun setShared() {
        status = "2"
    }

    @Ignore
    fun setUnShared() {
        status = "3"
    }

    //[是否下发]
    @Ignore
    fun isSend(): Boolean {
        return status == "0"
    }


    //[文本显示-地标点相关信息]
    @Ignore
    fun getPointLatString(): String? {
        return try {
            if (points.isNullOrEmpty()) null else points!![0].latString
        } catch (e: Exception) {
            null
        }
    }

    @Ignore
    fun getPointLngString(): String? {
        return try {
            if (points.isNullOrEmpty()) null else points!![0].lngString
        } catch (e: Exception) {
            null
        }
    }

    @Ignore
    fun getPointLngElevation(): String {
        try {
            if (points.isNullOrEmpty()) return "0"
            if (points!![0].elevation.isNullOrEmpty()) return "0"
            return points!![0].elevation!!
        } catch (e: Exception) {
            return "0"
        }
    }

    @Ignore
    fun getPointFormatDate(): String? {
        return try {
            if (createTime?.isNotEmpty() == true) {
                createTime!!.substring(0, "yyyy-MM-dd".length)
            } else UI.formatDate(Date(), "yyyy-MM-dd")
        } catch (e: Exception) {
            UI.formatDate(Date(), "yyyy-MM-dd")
        }
    }

    @Ignore
    fun getPointRemark(): String? {
        return try {
            if (points?.isNotEmpty() == true) {
                points!![0].remark
            } else null
        } catch (e: Exception) {
            null
        }
    }

}


@Entity(indices = [Index(value = ["name", "latitude", "longitude", "remark", "elevation", "iconType"], unique = true)])
@Parcelize
data class MapPoint(
    @ColumnInfo(name = "pointId")
    var pointId: String? = null,

    @ColumnInfo(name = "name")
    var name: String? = null,          //[名称]

    @ColumnInfo(name = "latitude")
    var latitude: Double? = null,
    @ColumnInfo(name = "longitude")
    var longitude: Double? = null,    //[经纬度]
    @ColumnInfo(name = "latString")
    var latString: String? = null,
    @ColumnInfo(name = "lngString")
    var lngString: String? = null,   //[经纬度度分秒]

    @ColumnInfo(name = "remark")
    var remark: String? = null,       //[备注]
    @ColumnInfo(name="elevation")
    var elevation: String? = null,   //[高程,单位m]
    @ColumnInfo(name = "pointType")
    var pointType: String? = null,   //[0点，1线点，2圆心，3多边型点]

    @ColumnInfo(name = "iconType")
    var iconType: String? = "0",    //[图标类型]

    @ColumnInfo(name = "arrivalTime")
    var arrivalTime: String? = null,  //[预达时间HH:mm:ss]

    @ColumnInfo(name = "obstacleAlt")
    var obstacleAlt: String? = null,  //[障碍物高，单位米]

    @ColumnInfo(name = "planeAlt")
    var planeAlt: String? = null,    //[飞行高度，单位米]

    @ColumnInfo(name = "securityAlt")
    var securityAlt: String? = null, //[安全高度，单位米]

    @ColumnInfo(name = "sort")
    var sort: String? = null,       //[排序，由小到大，默认0]

    @ColumnInfo(name = "trueSpeed")
    var trueSpeed: String? = null, //[真速，单位km/h]

    @ColumnInfo(name = "turningMode")
    var turningMode: String? = null, //[转弯方式]

    @ColumnInfo(name = "turningSlope")
    var turningSlope: String? = null, //[转弯坡度]

    @ColumnInfo(name = "time")
    var time: String? = null, //[航点、障碍物编辑时用户自己修改的时间]

): Parcelable

